﻿using System;
using System.Windows.Forms;
using GDFlacTool.Properties;

namespace GDFlacTool.Forms
{
    public partial class FormWaveMp3Settings : Form
    {
        private int _waveMp3BitrateValue;

        private string _waveMp3Bitrate = string.Empty;
        private string _waveMp3Encoding = string.Empty;
        private string _waveMp3Frequency = string.Empty;
        private string _waveMp3Channels = string.Empty;
        private string _waveMp3Copyright = string.Empty;
        private string _waveMp3Original = string.Empty;
        private string _waveMp3Crc = string.Empty;
        private string _waveMp3ForceIso = string.Empty;

        public FormWaveMp3Settings()
        {
            InitializeComponent();
        }

        public string WaveMp3Bitrate
        {
            get { return _waveMp3Bitrate; }
        }

        public string WaveMp3Encoding
        {
            get { return _waveMp3Encoding; }
        }

        public string WaveMp3Frequency
        {
            get { return _waveMp3Frequency; }
        }

        public string WaveMp3Channels
        {
            get { return _waveMp3Channels; }
        }

        public string WaveMp3Copyright
        {
            get { return _waveMp3Copyright; }
        }

        public string WaveMp3Original
        {
            get { return _waveMp3Original; }
        }

        public string WaveMp3Crc
        {
            get { return _waveMp3Crc; }
        }

        public string WaveMp3ForceIso
        {
            get { return _waveMp3ForceIso; }
        }

        private void FormWaveMp3Settings_Load(object sender, EventArgs e)
        {
            cboWaveMp3Encoding.SelectedIndex = AudioConverterSettings.Default.cboWaveMp3Encoding_SelectedIndex;
            trbWaveMp3Bitrate.Value = AudioConverterSettings.Default.trbWaveMp3Bitrate_Value;

            ConvertWaveMp3CbrValues();
        }

        private void FormWaveMp3Settings_FormClosing(object sender, FormClosingEventArgs e)
        {
            AudioConverterSettings.Default.cboWaveMp3Encoding_SelectedIndex = cboWaveMp3Encoding.SelectedIndex;
            AudioConverterSettings.Default.trbWaveMp3Bitrate_Value = trbWaveMp3Bitrate.Value;
        }

        private void btnMp3Advanced_Click(object sender, EventArgs e)
        {
            var frm = new FormConverterMp3(true);

            DialogResult result = frm.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                _waveMp3Frequency = frm.Frequency;
                _waveMp3Channels = frm.Channels;
                _waveMp3Copyright = frm.Copyright;
                _waveMp3Original = frm.Original;
                _waveMp3Crc = frm.CrcErrorProtection;
                _waveMp3ForceIso = frm.ForceIsoCompability;
            }
        }

        private void cboWaveMp3Encoding_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cboWaveMp3Encoding.SelectedIndex)
            {
                case 0:
                    _waveMp3Encoding = "-encoding={qt}SLOW{qt}";
                    break;
                case 1:
                    _waveMp3Encoding = string.Empty;
                    break;
                case 2:
                    _waveMp3Encoding = "-encoding={qt}FAST{qt}";
                    break;
                default:
                    _waveMp3Encoding = string.Empty;
                    break;
            }
        }

        private void trbWaveMp3Bitrate_ValueChanged(object sender, EventArgs e)
        {
            ConvertWaveMp3CbrValues();
        }

        private void ConvertWaveMp3CbrValues()
        {
            if (trbWaveMp3Bitrate.Value <= 8)
            {
                _waveMp3BitrateValue = trbWaveMp3Bitrate.Value * 8;

                if (trbWaveMp3Bitrate.Value < 4)
                {
                    lblMp3Info.Text = string.Format("[sample rates: 8, 11, 12, 16, 22 & 24 KHz]");
                }
                else
                {
                    lblMp3Info.Text = string.Empty;
                }
            }

            if (trbWaveMp3Bitrate.Value > 8 && trbWaveMp3Bitrate.Value <= 14)
            {
                _waveMp3BitrateValue = trbWaveMp3Bitrate.Value * 8 + (8 * (trbWaveMp3Bitrate.Value - 8));

                if (trbWaveMp3Bitrate.Value == 13)
                {
                    lblMp3Info.Text = string.Format("[sample rates: 8, 11, 12, 16, 22 & 24 KHz]");
                }
                else
                {
                    lblMp3Info.Text = string.Empty;
                }
            }

            if (trbWaveMp3Bitrate.Value == 15)
            {
                _waveMp3BitrateValue = 192;
                lblMp3Info.Text = string.Format("[sample rates: 32, 44.1 & 48 KHz]");
            }

            if (trbWaveMp3Bitrate.Value == 16)
            {
                _waveMp3BitrateValue = 224;
                lblMp3Info.Text = string.Format("[sample rates: 32, 44.1 & 48 KHz]");
            }

            if (trbWaveMp3Bitrate.Value == 17)
            {
                _waveMp3BitrateValue = 256;
                lblMp3Info.Text = string.Format("[sample rates: 32, 44.1 & 48 KHz]");
            }

            if (trbWaveMp3Bitrate.Value == 18)
            {
                _waveMp3BitrateValue = 320;
                lblMp3Info.Text = string.Format("[sample rates: 32, 44.1 & 48 KHz]");
            }

            lblMp3Bitrate.Text = string.Format("Constant Bit Rate: {0} kbps", _waveMp3BitrateValue);

            _waveMp3Bitrate = string.Format("-b {0}", _waveMp3BitrateValue);

        }

        
    }
}
